package com.oascarmovie.rpc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.oascarmovie.Constants;

public class RPCService {
  private final static String SERVER_URL = "http://taog/OscarMovie"; //masterDataAction!award
	private Context context;

	public RPCService(Context context) {
		this.context = context;
	}

	protected void call(String moduleName, String actionName, JSONObject bean, RPCServiceCallback callback) throws JSONException {
		String httpUrl = SERVER_URL + "/" + moduleName + "!" + actionName;

		HttpClient httpclient = null;
		HttpGet httppost = null;
		HttpResponse response = null;
		try {

			JSONObject json = new JSONObject();
			/*JSONObject userObj = new JSONObject();
			userObj.put(Constants.JSON_USEROBJ_SESSIONID, userInfoBean.getSessionId());
			userObj.put(Constants.JSON_USEROBJ_UID, userInfoBean.getUserId());
			userObj.put(Constants.JSON_USEROBJ_USERNAME, userInfoBean.getUserName());
			userObj.put(Constants.JSON_USEROBJ_PASSWORD, userInfoBean.getPassword());
			json.put(Constants.JSON_USEROBJ, userObj);

			json.put(Constants.JSON_BEAN, bean);
			JSONObject param = new JSONObject();
			param.put(Constants.JSON, json);

			String paramStr = CLSBase64.encode(param.toString().getBytes());
			Logger.log(context, "RPC send:" + param.toString());
			Logger.log(context, "RPC BASE64 encode:" + paramStr);*/

			httpclient = new DefaultHttpClient();
			httppost = new HttpGet(httpUrl);
			// BasicHttpParams parameters = new BasicHttpParams();
			// parameters.setParameter("json", paramStr);
			// httppost.setParams(parameters);
			response = httpclient.execute(httppost);

			int statusCode = response.getStatusLine().getStatusCode();
			switch (statusCode) {
			case 200:
				// ok
				processContent(response, callback);
				break;
			default:

			}

		}
		catch (ClientProtocolException e) {
			e.printStackTrace();
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}

		finally {

		}
	}

	private void processContent(HttpResponse response, RPCServiceCallback callback) {
		//UserInfoBean userInfoBean = ((Application) this.context.getApplicationContext()).getUserInfoBean();

		StringBuilder builder = new StringBuilder();

		BufferedReader bufferedReader = null;

		try {
			bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

			for (String s = bufferedReader.readLine(); s != null; s = bufferedReader.readLine()) {
				builder.append(s);
			}

		}
		catch (IOException e) {
			e.printStackTrace();
		}

		String getStr = builder.toString();
		//Logger.log(context, "RPC get:" + getStr);

		JSONObject jsonObj = null;
		JSONObject statusObj = null;
		JSONObject userObj = null;
		JSONObject bean = null;

		try {
			jsonObj = new JSONObject(getStr).getJSONObject(Constants.JSON);
			//statusObj = jsonObj.getJSONObject(Constants.JSON_STATUS);
			//userObj = jsonObj.getJSONObject(Constants.JSON_USEROBJ);
			//bean = jsonObj.getJSONObject(Constants.JSON_BEAN);

			//String statusType = statusObj.getString(Constants.JSON_STATUS_TYPE);
			// String statusMsg =
			// statusObj.getString(Constants.JSON_STATUS_MSG);

			//String sessionId = null;
			//String uid = null;
			/*if (userObj != null) {
				sessionId = userObj.getString(Constants.JSON_USEROBJ_SESSIONID);
				userInfoBean.setSessionId(sessionId);

				uid = userObj.getString(Constants.JSON_USEROBJ_UID);
				userInfoBean.setUserId(uid);

				String username = userObj.getString(Constants.JSON_USEROBJ_USERNAME);
				userInfoBean.setUserName(username);

				String password = userObj.getString(Constants.JSON_USEROBJ_PASSWORD);
				userInfoBean.setPassword(password);
			}*/
			/*if (sessionId != null && uid != null) {
				if (Constants.JSON_STATUS_TYPE_SUCCESS.equalsIgnoreCase(statusType)) {
					callback.onSuccess(bean);
				}
				else if (Constants.JSON_STATUS_TYPE_WARNING.equalsIgnoreCase(statusType)) {
					callback.onSuccess(bean);
				}
				else {
					callback.onFailure(bean);
				}
			}
			else {

			}*/

		}
		catch (JSONException e) {
			e.printStackTrace();
		}

	}
}
